#pragma once
#ifndef _CSTR_YAML_H__
#define _CSTR_YAML_H__

#include "cstr.h"

// The Module is all about string check operations.
typedef unsigned char uchar;

/*
 * Check the octet at the specified position.
 */

#define CHECK_AT(string,octet,offset)                                   \
    ((string).start[offset] == (uchar)(octet))

/*
 * Check the current octet in the buffer.
 */

#define CHECK(string,octet) CHECK_AT((string),(octet),0)

/*
 * Check if the character at the specified position is an alphabetical
 * character, a digit, '_', or '-'.
 */

#define IS_ALPHA_AT(string,offset)                                      \
     (((string).start[offset] >= (uchar) '0' &&                         \
       (string).start[offset] <= (uchar) '9') ||                        \
      ((string).start[offset] >= (uchar) 'A' &&                         \
       (string).start[offset] <= (uchar) 'Z') ||                        \
      ((string).start[offset] >= (uchar) 'a' &&                         \
       (string).start[offset] <= (uchar) 'z') ||                        \
      (string).start[offset] == '_' ||                                  \
      (string).start[offset] == '-')

#define IS_ALPHA(string)    IS_ALPHA_AT((string),0)

/*
 * Check if the character at the specified position is a digit.
 */

#define IS_DIGIT_AT(string,offset)                                      \
     (((string).start[offset] >= (uchar) '0' &&                         \
       (string).start[offset] <= (uchar) '9'))

#define IS_DIGIT(string)    IS_DIGIT_AT((string),0)

/*
 * Get the value of a digit.
 */

#define AS_DIGIT_AT(string,offset)                                      \
     ((string).start[offset] - (uchar) '0')

#define AS_DIGIT(string)    AS_DIGIT_AT((string),0)

/*
 * Check if the character at the specified position is a hex-digit.
 */

#define IS_HEX_AT(string,offset)                                        \
     (((string).start[offset] >= (uchar) '0' &&                         \
       (string).start[offset] <= (uchar) '9') ||                        \
      ((string).start[offset] >= (uchar) 'A' &&                         \
       (string).start[offset] <= (uchar) 'F') ||                        \
      ((string).start[offset] >= (uchar) 'a' &&                         \
       (string).start[offset] <= (uchar) 'f'))

#define IS_HEX(string)    IS_HEX_AT((string),0)

/*
 * Get the value of a hex-digit.
 */

#define AS_HEX_AT(string,offset)                                        \
      (((string).start[offset] >= (uchar) 'A' &&                        \
        (string).start[offset] <= (uchar) 'F') ?                        \
       ((string).start[offset] - (uchar) 'A' + 10) :                    \
       ((string).start[offset] >= (uchar) 'a' &&                        \
        (string).start[offset] <= (uchar) 'f') ?                        \
       ((string).start[offset] - (uchar) 'a' + 10) :                    \
       ((string).start[offset] - (uchar) '0'))
 
#define AS_HEX(string)  AS_HEX_AT((string),0)
 
/*
 * Check if the character is ASCII.
 */

#define IS_ASCII_AT(string,offset)                                      \
    ((string).start[offset] <= (uchar) '\x7F')

#define IS_ASCII(string)    IS_ASCII_AT((string),0)

/*
 * Check if the character can be printed unescaped.
 */

#define IS_PRINTABLE_AT(string,offset)                                        \
    (((string).start[offset] == 0x0A)         /* . == #x0A */                 \
     || ((string).start[offset] >= 0x20       /* #x20 <= . <= #x7E */         \
         && (string).start[offset] <= 0x7E)                                   \
     || ((string).start[offset] == 0xC2       /* #0xA0 <= . <= #xD7FF */      \
         && (string).start[offset+1] >= 0xA0)                                 \
     || ((string).start[offset] > 0xC2                                        \
         && (string).start[offset] < 0xED)                                    \
     || ((string).start[offset] == 0xED                                       \
         && (string).start[offset+1] < 0xA0)                                  \
     || ((string).start[offset] == 0xEE)                                      \
     || ((string).start[offset] == 0xEF      /* #xE000 <= . <= #xFFFD */      \
         && !((string).start[offset+1] == 0xBB        /* && . != #xFEFF */    \
             && (string).start[offset+2] == 0xBF)                             \
         && !((string).start[offset+1] == 0xBF                                \
             && ((string).start[offset+2] == 0xBE                             \
                 || (string).start[offset+2] == 0xBF))))

#define IS_PRINTABLE(string)    IS_PRINTABLE_AT((string),0)

/*
 * Check if the character at the specified position is NUL.
 */

#define IS_Z_AT(string,offset)    CHECK_AT((string),'\0',(offset))

#define IS_Z(string)    IS_Z_AT((string),0)

/*
 * Check if the character at the specified position is BOM.
 */

#define IS_BOM_AT(string,offset)                                                \
     (CHECK_AT((string),'\xEF',(offset))                                        \
      && CHECK_AT((string),'\xBB',(offset)+1)                                   \
      && CHECK_AT((string),'\xBF',(offset)+2))  /* BOM (#xFEFF) */

#define IS_BOM(string)  IS_BOM_AT(string,0)

/*
 * Check if the character at the specified position is space.
 */

#define IS_SPACE_AT(string,offset)  CHECK_AT((string),' ',(offset))

#define IS_SPACE(string)    IS_SPACE_AT((string),0)

/*
 * Check if the character at the specified position is tab.
 */

#define IS_TAB_AT(string,offset)    CHECK_AT((string),'\t',(offset))

#define IS_TAB(string)  IS_TAB_AT((string),0)

/*
 * Check if the character at the specified position is blank (space or tab).
 */

#define IS_BLANK_AT(string,offset)                                              \
    (IS_SPACE_AT((string),(offset)) || IS_TAB_AT((string),(offset)))

#define IS_BLANK(string)    IS_BLANK_AT((string),0)

/*
 * Check if the character at the specified position is a line break.
 */

#define IS_BREAK_AT(string,offset)                                              \
    (CHECK_AT((string),'\r',(offset))               /* CR (#xD)*/               \
     || CHECK_AT((string),'\n',(offset))            /* LF (#xA) */              \
     || (CHECK_AT((string),'\xC2',(offset))                                     \
         && CHECK_AT((string),'\x85',(offset)+1))   /* NEL (#x85) */            \
     || (CHECK_AT((string),'\xE2',(offset))                                     \
         && CHECK_AT((string),'\x80',(offset)+1)                                \
         && CHECK_AT((string),'\xA8',(offset)+2))   /* LS (#x2028) */           \
     || (CHECK_AT((string),'\xE2',(offset))                                     \
         && CHECK_AT((string),'\x80',(offset)+1)                                \
         && CHECK_AT((string),'\xA9',(offset)+2)))  /* PS (#x2029) */

#define IS_BREAK(string)    IS_BREAK_AT((string),0)

#define IS_CRLF_AT(string,offset)                                               \
     (CHECK_AT((string),'\r',(offset)) && CHECK_AT((string),'\n',(offset)+1))

#define IS_CRLF(string) IS_CRLF_AT((string),0)

/*
 * Check if the character is a line break or NUL.
 */

#define IS_BREAKZ_AT(string,offset)                                           \
    (IS_BREAK_AT((string),(offset)) || IS_Z_AT((string),(offset)))

#define IS_BREAKZ(string)   IS_BREAKZ_AT((string),0)

/*
 * Check if the character is a line break, space, or NUL.
 */

#define IS_SPACEZ_AT(string,offset)                                           \
    (IS_SPACE_AT((string),(offset)) || IS_BREAKZ_AT((string),(offset)))

#define IS_SPACEZ(string)   IS_SPACEZ_AT((string),0)

/*
 * Check if the character is a line break, space, tab, or NUL.
 */

#define IS_BLANKZ_AT(string,offset)                                           \
    (IS_BLANK_AT((string),(offset)) || IS_BREAKZ_AT((string),(offset)))

#define IS_BLANKZ(string)   IS_BLANKZ_AT((string),0)

/*
 * Determine the width of the character.
 */

#define WIDTH_AT(string,offset)                                               \
     (((string).start[offset] & 0x80) == 0x00 ? 1 :                           \
      ((string).start[offset] & 0xE0) == 0xC0 ? 2 :                           \
      ((string).start[offset] & 0xF0) == 0xE0 ? 3 :                           \
      ((string).start[offset] & 0xF8) == 0xF0 ? 4 : 0)

#define WIDTH(string)   WIDTH_AT((string),0)

#endif  // _CSTR_YAML_H__
